package com.hope.onlinestudy.activity.info

import android.app.AlertDialog
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.hope.onlinestudy.BuildConfig
import com.hope.onlinestudy.R
import com.hope.onlinestudy.img.GlideCircleTransform
import com.hope.onlinestudy.model.UserInfoModel
import com.hope.onlinestudy.mvp.MVPBaseActivity
import com.hope.onlinestudy.utils.ApiUtils
import kotlinx.android.synthetic.main.activity_info.*
import kotlinx.android.synthetic.main.view_title.*

/**
 * 邮箱 kazeik@163.com qq:77132995
 */
class InfoActivity : MVPBaseActivity<InfoContract.View, InfoPresenter>(), View.OnClickListener,
    InfoContract.View {

    private var sex: Int = 0
    private var isEdit: Boolean = false

    override fun getLayoutView(): Int {
        return R.layout.activity_info
    }

    override fun initData() {
        tv_title.text = "我的资料"
        iv_backup.setOnClickListener(this)
        iv_right.setOnClickListener(this)
        iv_right.visibility = View.VISIBLE
        iv_right.setImageResource(R.drawable.ic_edit)

        val data = intent.getSerializableExtra("data")
        if (null != data) {
            val usermodel = data as UserInfoModel
            showInfo(usermodel)
        }
        eidtInfo(isEdit)
    }

    private fun showInfo(usermodel: UserInfoModel?) {
        Glide.with(this).load("${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_file_context}/${usermodel?.userImg}").asBitmap()
            .placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).override(100, 100)
            .transform(GlideCircleTransform(this)).into(ivUserIcon)
        tvUserAccount.text = if (TextUtils.isEmpty(usermodel?.username)) "" else usermodel?.username
        tvUserName.setText(if (TextUtils.isEmpty(usermodel?.truename)) "" else usermodel?.truename)
        tvSex.text =
            if (usermodel?.usersex == "2") "女" else if (usermodel?.usersex == "1") "男" else ""
        tvTel.setText(
            if (TextUtils.isEmpty(usermodel?.mobilephone) || TextUtils.equals(
                    "null",
                    usermodel?.mobilephone
                )
            ) "" else usermodel?.mobilephone
        )
        tvEmail.setText(if (TextUtils.isEmpty(usermodel?.useremail)) "" else usermodel?.useremail)
        tvSchool.setText(if (TextUtils.isEmpty(usermodel?.companyName)) "" else usermodel?.companyName)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_backup -> finish()
            R.id.iv_right -> {
                iv_right.setImageResource(if (!isEdit) R.drawable.ic_save else R.drawable.ic_edit)
                eidtInfo(!isEdit)
                if (isEdit)
                    updateinfo()

                isEdit = !isEdit
            }
            R.id.tvSex -> showlistDialog()
        }
    }

    private fun showlistDialog() {
        val dialog = AlertDialog.Builder(this)
        dialog.setTitle("请选择性别")
        val sexList = arrayOf("男", "女")
        dialog.setItems(
            sexList
        ) { _, which ->
            tvSex.text = sexList[which]
            sex += which + 1
        }
        dialog.show()
    }

    private fun updateinfo() {
        val accout = tvUserAccount.text.toString()
        val userName = tvUserName.text.toString()
        val tel = tvTel.text.toString()
        val email = tvEmail.text.toString()
        mPresenter?.updateInfo(accout, userName, tel, email, sex)
    }

    private fun eidtInfo(falg: Boolean) {
        tvUserName.isFocusable = falg
        tvUserName.isFocusableInTouchMode = falg

        tvTel.isFocusable = falg
        tvTel.isFocusableInTouchMode = falg

        tvEmail.isFocusable = falg
        tvEmail.isFocusableInTouchMode = falg

        tvSex.setOnClickListener(if (falg) this else null)
    }

}